<?php
/**
 * 巡展基金-项目-线下活动
 * @author Administrator
 *
 */
class xPOfflineModel extends MY_Model{
    function xPOfflineModel(){
        parent::MY_Model();
        $this->table = 'XPOffline';
    }
    /**
     * 重新组装数据(non-PHPdoc)
     * @see MY_Model::getNewList()
     */
    function getNewList($where = '', $limit = NULL, $offset = NULL, $colum = '', $orderby = ''){
        $findList = parent::getNewList($where, $limit, $offset, $colum, $orderby);
        $list = array();
        foreach ($findList as $v){
            if (isset($v['costNo'])){
                $list[$v['costNo']] = $v;
            }else{
                $list[] = $v;
            }
        }
        return $list;
    }
    /**
     * 更新父类金额(non-PHPdoc)
     * @see MY_Model::updateParentMoneys()
     */
    function updateParentMoneys($parentId){
        $this->CI->load->model('xProjectModel');
        $this->CI->xProjectModel->updateMoney($parentId);
    }
    /**
     * 获取父级关联ID(non-PHPdoc)
     * @see MY_Model::getParentId()
     */
    function getParentId($data){
        return isset($data['activityId']) && $data['activityId'] ? $data['activityId'] : 0;
    }
    /**
     * 包装数据(non-PHPdoc)
     * @see MY_Model::packing()
     */
    function packing($list = array()){
        //角色列表
        $roleList = $this->config->item('roleList');
        //审核结果
        $auditRs = $this->config->item('auditRs');
        foreach ($list as $k => $v){
            //结算审核状态
            $list[$k]['settleAuditStr'] = isset($roleList[$v['settleAuditRole']]) ? $roleList[$v['settleAuditRole']].'已审核' : '';
            if ($list[$k]['settleAuditStr']){
                $list[$k]['settleAuditStr'] .= isset($auditRs[$v['settleAuditRs']]) ? ','.$auditRs[$v['settleAuditRs']] : '';
            }else{
                $list[$k]['settleAuditStr'] = isset($auditRs[$v['settleAuditRs']]) ? $auditRs[$v['settleAuditRs']] : '';
            }
        }
        return $list;
    }
    /**
     * 更新线下活动金额
     * @param string $offlineCostNo
     * @return boolean
     */
    function updateMoney($offlineCostNo){
        $findData = $this->getData(array('costNo'=>$offlineCostNo));
        $this->CI->load->model('xPODetailModel');
        //$this->CI->load->model('xPODetailModel');
        $childMoney = $this->CI->xPODetailModel->getMoney($offlineCostNo);
        $data = array(
            'budgetApply'   => $childMoney['budgetApply'],
            'settleApply'   => $childMoney['settleApply'],
        );
        //结算审核金额
        $data['settleAudit'] = $findData['settleAuditRs'] == 1 ? $data['settleApply'] : 0;
        $this->edit(array('costNo'=>$offlineCostNo), $data);
    }
    /**
     * 统计对应父级ID的金额
     * @param int $projectId
     * @return array
     */
    function getMoney($activityId){
        $data = array(
            'budgetApply' => 0,//预算申请金额
            'budgetAudit' => 0,//预算审核金额（RMP巡展暂时无预算审核）
            'settleApply' => 0,//结算申请金额
            'settleAudit' => 0,//结算审核金额
        );
        $findList = $this->getList(array('activityId'=>$activityId));
        foreach ($findList as $v){
            $data['budgetApply'] += $v['budgetApply'];
            $data['settleApply'] += $v['settleApply'];
            in_array($v['settleAuditRs'], array(1,4)) && $data['settleAudit'] += $v['settleAudit'];
        }
        return $data;
    }
    /**
     * 结算审核状态统计
     * @param int $activityId 活动ID
     * @return multitype:
     */
    function getSettleStatusCount($activityId) {
        $findList = $this->getList(array('activityId'=>$activityId/*,'settleApply > '=>0*/), null, null, 'settleAuditRs');
        $count = array();
        foreach ($findList as $v){
            $count[$v['settleAuditRs']] ++;
        }
        return $count;
    }
    /**
     * 线下活动通过项目ID一键结算审核
     * @param int $activityId
     * @param number $audit
     * @param array $user
     */
    function settleAuditByActivityId($activityId, $audit = 1,$user = array()){
        $findList = $this->getList(array('activityId'=>$activityId));
        foreach ($findList as $v){
            $auditData = array(
                'settleAudit' => $audit==1 ? $v['settleApply'] : 0,
                'settleAuditRs' => $audit,
                'settleAuditRole' => $user['userRole'],
                'settleAuditTime' => time(),
            );
            $this->db->update($this->table, $auditData, array('costNo'=>$v['costNo']));
        }
    }
    /**
     * 线下活动单个审核
     * @param number $costNo 线下活动流水号
     * @param number $audit
     * @param array $user
     * @return object
     */
    function settleAudit($costNo, $audit = 1, $user = array()){
        $findData = $this->getData(array('costNo'=>$costNo));
        $auditData = array(
            'settleAudit' =>  $audit==1 ? $findData['settleApply'] : 0,
            'settleAuditRs' => $audit,
            'settleAuditRole' => $user['userRole'],
            'settleAuditTime' => time(),
        );
        return $this->db->update($this->table, $auditData,array('costNo'=>$costNo));
    }
}